package cn.ismartv.speedtester.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;

/**
 * Created by huaijie on 1/8/15.
 */
public class SakuraViewPager extends ViewPager {
    private final Rect mTempRect = new Rect();

    public SakuraViewPager(Context context) {
        super(context);
    }

    public SakuraViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


//    @Override
//    public boolean arrowScroll(int direction) {
//        View currentFocused = findFocus();
//        if (currentFocused == this) {
//            currentFocused = null;
//        } else if (currentFocused != null) {
//            boolean isChild = false;
//            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
//                 parent = parent.getParent()) {
//                if (parent == this) {
//                    isChild = true;
//                    break;
//                }
//            }
//            if (!isChild) {
//                // This would cause the focus search down below to fail in fun ways.
//                final StringBuilder sb = new StringBuilder();
//                sb.append(currentFocused.getClass().getSimpleName());
//                for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
//                     parent = parent.getParent()) {
//                    sb.append(" => ").append(parent.getClass().getSimpleName());
//                }
//                currentFocused = null;
//            }
//        }
//
//        boolean handled = false;
//
//        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
//                direction);
//        if (nextFocused != null && nextFocused != currentFocused) {
//            if (direction == View.FOCUS_LEFT) {
//                // If there is nothing to the left, or this is causing us to
//                // jump to the right, then what we really want to do is page left.
//                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
//                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
//                if (currentFocused != null && nextLeft >= currLeft) {
//                    handled = false;
//                } else {
//                    handled = nextFocused.requestFocus();
//                }
//            } else if (direction == View.FOCUS_RIGHT) {
//                // If there is nothing to the right, or this is causing us to
//                // jump to the left, then what we really want to do is page right.
//                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
//                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
//                if (currentFocused != null && nextLeft <= currLeft) {
//                    handled = false;
//                } else {
//                    handled = nextFocused.requestFocus();
//                }
//            }
//        } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
//            // Trying to move left and nothing there; try to page.
//            handled = false;
//        } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
//            // Trying to move right and nothing there; try to page.
//            handled = false;
//        }
//        if (handled) {
//            playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
//        }
//        return handled;
//    }

    private Rect getChildRectInPagerCoordinates(Rect outRect, View child) {
        if (outRect == null) {
            outRect = new Rect();
        }

        if (child == null) {
            outRect.set(0, 0, 0, 0);
            return outRect;
        } else {
            outRect.left = child.getLeft();
            outRect.right = child.getRight();
            outRect.top = child.getTop();
            outRect.bottom = child.getBottom();

            ViewGroup group;
            for (ViewParent parent = child.getParent(); parent instanceof ViewGroup && parent != this; parent = group.getParent()) {
                group = (ViewGroup) parent;
                outRect.left += group.getLeft();
                outRect.right += group.getRight();
                outRect.top += group.getTop();
                outRect.bottom += group.getBottom();
            }

            return outRect;
        }
    }


}
